package com.android.project.games;

import java.util.ArrayList;
import java.util.List;

import android.os.Vibrator;
import android.util.Log;
import android.view.inputmethod.InputMethodManager;

import com.android.project.framework.math.Vector2;
import com.android.project.mysql.connection.AssociationWord;



public class World2 {
	public interface WorldListener{
		public void semi();
		public void wrong();
		public void right();
	
	}
	
	public static final Vector2 gravity = new Vector2(0, -10);
//	public static final float WORLD_WIDTH = 10;
	//public static final float WORLD_HEIGHT = 15 * 4;
	public static final float WORLD_WIDTH = 320;
	public static final float WORLD_HEIGHT = 480;
	public static final int WORLD_STATE_READY = 0;
	public static final int WORLD_STATE_RUNNING = 1;
	public static final int WORLD_STATE_GAME_PAUSED = 2;
	public static final int WORLD_STATE_LEVEL_END = 3;
	public static final int WORLD_GAME_OVER = 4;

	public List<Label> labels;
	int score;
	WorldListener listener;
	int state;
	Label newLabel;
    int level = 1;
	boolean keyboardShow = false;
	float render = 0;
	boolean pressBack = false;
	

	public World2(WorldListener listener){
		
		labels = new ArrayList<Label>();
		this.listener = listener;
		this.score = 40;
		this.state = WORLD_STATE_READY;
		render = 0;
		keyboardShow= false;
	
	}
	
	public void generateLevel(String w, float deltaTime){
	
		if(labels.isEmpty()){	
			
			newLabel = new Label(160, 460);
			newLabel.setWord(w);
			newLabel.state = Label.LABEL_STATE_INPUT;
			labels.add(newLabel);
			updateLabel(deltaTime);
			Assets.playSound(Assets.guillotineSound);
					
			
		}
		else{	
			if(labels.size() < 5) {
				float x = newLabel.position.x;
				float y = newLabel.position.y;
				newLabel = new Label(x, y + 32);
				newLabel.setWord("");
				labels.add(newLabel);
			}
			updateLabel(deltaTime);
		}
	}

	public void update(float deltaTime){
		updateWorld(deltaTime);
		checkLevelEnd();
		checkGameOver();


	}

	private void checkLevelEnd() {
		// TODO Auto-generated method stub
		
		int i = labels.size();
		//Log.d("world2 checkLevel", "" + i);
		if(i == 5 && labels.get(i - 1).state != Label.LABEL_STATE_INSERT){
			state = WORLD_STATE_LEVEL_END;
			keyboardShow = false;
			render = 0;
		}
		
	}

	public void updateWorld(float deltaTime) {
		// TODO Auto-generated method stub
		if(pressBack){
			keyboardShow = false;
			render = 0;
			return;
		}
		if(this.state == WORLD_STATE_GAME_PAUSED){
			render = 0;
			keyboardShow= false;
			return;
			
		}
		else if(this.state == WORLD_STATE_RUNNING && !keyboardShow){
			render = 1;
			keyboardShow= true;
			return;
		}
		else if(this.state == WORLD_STATE_LEVEL_END){
			keyboardShow = false;
			render = 0;
			return;
		}
		else if(this.state == WORLD_STATE_RUNNING && keyboardShow){
			render = 3;
			return;
		}
	
	}

	private void checkGameOver() {
		// TODO Auto-generated method stub
		if(this.score == 0){
			state = WORLD_GAME_OVER;
			render = 0;
			keyboardShow = false;
			
		}
		
		
	}

	public List<Label> getLabels() {
		return labels;
	}


	private void updateLabel(float deltaTime) {
		// TODO Auto-generated method stub
		/*for(int i = 0; i < labels.size(); i++){
			Label label = labels.get(i);
			if(label.state == Label.LABEL_STATE_INPUT){
				if(label.position.y <= 32)
					label.hitGround();
				
				
				else{
				
					label.update(deltaTime);
				}
				
				
				
			}
			if(label.state == Label.LABEL_STATE_INSERT){
				Label prev = labels.get(i - 1);
				if(label.position.y - prev.position.y <= 45)
					//label.hitGround();
				else{
					//label.velocity.y = Label.LABEL_VELOCITY * 1.5f;
					label.update(deltaTime);
				}
				
			}			
		}	*/
		
	}
			
	public int checkWord(List<AssociationWord> words){
		this.render = 2;
		for(int i = 1; i < labels.size(); i++){
			Label l = labels.get(i);
			String check = words.get(1).getWord();
			if(check.equalsIgnoreCase(l.getWord()) && l.writeEnable){
				score += 50;
				l.state = Label.LABEL_STATE_RIGHT;
				listener.right();
				l.writeEnable = false;
				return 0;
			}
			if(l.writeEnable ){
				for(int j= 2; j < words.size(); j++){
					Log.d("world2", " "+ words.get(j).getWord());
					if(words.get(j).getWord().equals(l.getWord())){
						score -= 5;
						l.state = Label.LABEL_STATE_SEMI;
						l.writeEnable = false;
						listener.semi();
						return j;
					}
				}	
				score -= 10;			
				l.state = Label.LABEL_STATE_WRONG;
				listener.wrong();
				l.writeEnable = false;
				return -1;
			}
		}
		return -1;
	}

	public void enableLabel(String s) {
		// TODO Auto-generated method stub
		int m = labels.size() ;
		for(int i = 0; i < m; i++){
			if(labels.get(i).state == Label.LABEL_STATE_INSERT) {
				labels.get(i).setWord(s);
				labels.get(i).writeEnable = true;
				return;
			}
				
		}
		
	}


}
